package com.huike.web.controller.clues;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.service.ITbClueService;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 线索跟进记录Controller
 * @date 2021-04-22
 */
@RestController
@RequestMapping("/clues/record")
public class TbClueTrackRecordController extends BaseController {
    @Autowired
    private ITbClueTrackRecordService clueTrackRecordService;


    /**
     * 查询线索跟进记录列表
     */
    @PreAuthorize("@ss.hasPermi('clues:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam("clueId")Long clueId) {
        System.out.println("跟进线索id:"+clueId);

//        根据线索id数据库查询跟进线索
        List<ClueTrackRecordVo> list = clueTrackRecordService.selectByClueId(clueId);

        return getDataTable(list);
    }


    /**
     * 新增线索跟进记录
     */
    @PreAuthorize("@ss.hasPermi('clues:record:add')")
    @Log(title = "线索跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClueTrackRecordVo tbClueTrackRecord) {

        System.out.println("添加线索跟踪记录tbClueTrackRecord："+tbClueTrackRecord);
        //构建新的对象
        TbClueTrackRecord tbClueTrackRecord1 = new TbClueTrackRecord();
        //复制基本字段
        BeanUtils.copyProperties(tbClueTrackRecord,tbClueTrackRecord1);
        //设置跟进时间
        tbClueTrackRecord1.setCreateTime(DateUtils.getNowDate());
        //获取跟进操作的人
        tbClueTrackRecord1.setCreateBy(SecurityUtils.getUsername());

        System.out.println("==============================="+tbClueTrackRecord1.toString());
        //        线索跟进过程中 发下手机号等信息发现改变，所以需要同步修改线索相关信息，构建线索对象

        TbClue tbClue = new TbClue();
        //复制基本字段

        BeanUtils.copyProperties(tbClueTrackRecord,tbClue);
//        tbClue.setSubject(tbClueTrackRecord.getSubject());
//        tbClue.setLevel(tbClueTrackRecord.getLevel());
//        tbClue.setName(tbClueTrackRecord.getName());
//        tbClue.setQq(tbClueTrackRecord.getQq());
//        tbClue.setWeixin(tbClueTrackRecord.getWeixin());
//        tbClue.setSex(tbClueTrackRecord.getSex());
//        tbClue.setAge(tbClueTrackRecord.getAge());
        //复制基本字段里面id
        tbClue.setId(tbClueTrackRecord.getClueId());

        //设置状态
         tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue()); //状态：进行中
        //        执行修改、添加保存操作
        clueTrackRecordService.insertTbClueTrackRecord(tbClue,tbClueTrackRecord1);

        return AjaxResult.success("添加线索跟踪成功");





    }
}
